<?php
/**
 * Description of DbAfdeling
 * @author Nick Swartele
 * 
 */

require_once 'class/defs/constants.php';
require_once FILE_DB_DATABASE;

class DbProject extends Database{
    
    private $id = '';
    private $naam = '';
    private $locatie = '';
    private $leidinggevende = '';
  
     public function __construct($id = ''){
        if (DEBUG_LVL & DEBUG_LVL_CLASS) //echo __FILE__ . __LINE__ .': ' . __CLASS__ .'<BR/>';
        
        parent::__construct();
      
        if (!empty ($id)){
            $dbProject = $this->DbProject($id);
        }
        else {
            $this->reset();
        }
    } 
    
     public function DbProject($id) {
        
    }
    
     public function getNaam() {

        return $this->naam;
    }
    
      public function save ($naam, $locatie, $leidinggevende) {
          if (!empty ($naam)){
            
            $query = "INSERT INTO `project`(`naam`, `locatie`, `leidinggevende`)
                        VALUES (
                            '" . mysql_real_escape_string($naam) . "',
                            '" . mysql_real_escape_string($locatie) . "',
                            '" . mysql_real_escape_string($leidinggevende) . "'
                            )";
            if (DEBUG_LVL & DEBUG_LVL_QUERY)  echo __FILE__ . ' ' . __LINE__ .': ' .$query . '<br />';

            if (!$this->dbquery($query) || mysql_affected_rows() == -1){
               throw new Exception(DB_ERROR_NIETVERWERKT . DB_ERROR_NIETVERWERKTFK);
            }
            
            // Update Class Attributes
            $this->naam = $naam;
            $this->locatie = $locatie;
            $this->leidinggevende = $leidinggevende;
          
            return TRUE;
        }
        else {
          throw new Exception(DB_ERROR_INVOERFOUT);
        }
    }
   
     public function update ($id, $naam, $locatie, $leidinggevende){
              
        if (!empty ($id)
            ){
            
            $query = "UPDATE project
                        SET naam =                   '" . mysql_real_escape_string($naam) . "', 
                            locatie =                '" . mysql_real_escape_string($locatie) . "', 
                            leidinggevende =         '" . mysql_real_escape_string($leidinggevende) . "'
                        WHERE project . id =         '" . mysql_real_escape_string($id) . "'";
       
                  if (DEBUG_LVL & DEBUG_LVL_QUERY)  echo __FILE__ . ' ' . __LINE__ .': ' .$query . '<br />';
            
            if (!$this->dbquery($query)){
                    throw new Exception(DB_ERROR_NIETVERWERKT);
            }
        
            // Update Class Attributes
            $this->naam = $naam;
            $this->locatie = $locatie;
            $this->leidinggevende = $leidinggevende;
            
            return TRUE;
        }
        else {
             throw new Exception(DB_ERROR_INVOERFOUT);
        }
    }
      public function selectOverzicht(){
          
         if($this->dbquery("SELECT * FROM `project`  LIMIT 0, 100 ")) {
             // Controle of er iets gevonden is in de database
             if($this->dbNumRows() >= 1){
                 
                     return $this->dbFetchAll();
                     
             } else {
                 echo DBBEDRIJF_ERROR_NOROW;
             }
             
         } else {
             throw new Exception (DB_ERROR_COULDNOTSELECT);
             
         }
    
          if (DEBUG_LVL & DEBUG_LVL_QUERY)  echo __FILE__ . ' ' . __LINE__ .': ' .$query . '<br />';
        
        $this->reset();
         
    }
    
    public function delete($id){
        
        $query = "DELETE FROM project WHERE project . id = '" . mysql_real_escape_string($id) . "'  LIMIT 1 ";
      
        if (!$this->dbquery($query)){
            throw new Exception(DB_ERROR_COULDNOTDELETE);
        }

        if (DEBUG_LVL & DEBUG_LVL_QUERY)  echo __FILE__ . ' ' . __LINE__ .': ' .$query . '<br />';
        
        $this->reset();
    }
    
       private function reset(){
        $this->naam = '';

    }


}
?>